﻿using System;
using System.Web;
using Newtonsoft.Json;
using eCommerce.core.Helpers;
using eCommerce.db.BORespository;
using eCommerce.db.DataRespository;
using eCommerce.log;

namespace eCommerce.db.Action
{
    public class IeDetailAction : IBaseAction
    {
        public void Do(HttpContext context)
        {
            var actionName = Utils.GetIdFromRequestString(Utils.Action, string.Empty);

            var responseData = new ResponseData();
            switch (actionName)
            {
                case "distinctproductbyie":
                    responseData = GetListDistinctProductByIeId();
                    break;

                case "delete":
                    responseData = Delete();
                    break;

                case "":
                    responseData.Success = false;
                    responseData.Message = "Can not found this action.";
                    break;
            }

            var jSonString = JsonConvert.SerializeObject(responseData, Utils.SerializeFormat("MM/dd/yyyy HH:mm:ss"));
            if (!string.IsNullOrEmpty(context.Request.Params["callback"]))
                jSonString = string.Format("{0}({1})", context.Request.Params["callback"], jSonString);
            context.Response.Write(jSonString);
        }

        public static ResponseData GetListDistinctProductByIeId()
        {
            var responseData = new ResponseData();
            var ieId = Utils.GetIdFromRequestString(Utils.IeId, 0);

            try
            {
                var lstIeDetail = IeDetailBo.ListDistinctProductByIeId(ieId);
                for (int i = 0; i < lstIeDetail.Count; i++)
                {
                    lstIeDetail[i].ItemIndex       = Utils.CalcItemIndex(i, Constant.PageIndex, lstIeDetail.Count);
                    lstIeDetail[i].SizeAndQuantity = IeDetailBo.SizeAndQuantityByProduct(ieId, lstIeDetail[i].ProductId);
                }

                responseData.Success = true;
                responseData.Data = lstIeDetail;
            }
            catch (Exception ex)
            {
                responseData.Success = false;
                responseData.Message = ex.Message;
                Logger.WriteLog(Logger.LogType.Error, ex.Message);
            }

            return responseData;
        }

        private static ResponseData Delete()
        {
            var responseData = new ResponseData();

            var ieId      = Utils.GetIdFromRequestString(Utils.IeId, 0);
            var productId = Utils.GetIdFromRequestString(Utils.ProductId, 0);

            try
            {
                IeDetailBo.Delete(ieId, productId);

                responseData.Success = true;
                responseData.Data = null;
                responseData.TotalRow = 0;
            }
            catch (Exception ex)
            {
                responseData.Success = false;
                responseData.Message = ex.Message;
                Logger.WriteLog(Logger.LogType.Error, ex.Message);
            }
            return responseData;
        }
    }
}
